package com.yc;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Map;

public class Demo {

    public static void main(String[] args) throws IOException {
//        captureJobPage("http://www.07347.com/jobs/321.html");
//        captureJobPage("http://www.07347.com/jobs/326.html");
        //captureJobs("http://www.07347.com/jobs/jobs_list/page/1.html");
//        for (int i = 1; i < 5; i++) {
//            captureJobs("http://www.07347.com/jobs/jobs_list/page/"+(i+1)+".html");
//        }

        downPage("https://www.jiushujie.com/sell/439065537", "d:/a/jsj");
    }

    public static void downPage(String addr, String dir) throws IOException {
        // 下载页面
        URLDemo.download(addr, dir);
        URL url = new URL(addr);
        Document doc = Jsoup.parse(url, 500);
        // CSS
        Elements links = doc.select("link");
        for (Element link : links) {
            if("stylesheet".equals(link.attr("rel"))){
                String href = link.attr("href");
                String path = getPath(href, url);
                URLDemo.download(path, dir + "/css");
            }
        }
        // JS
        Elements scripts = doc.select("script");
        for (Element script : scripts) {
            if(script.attr("src")!=null){
                String href = script.attr("src");
                String path = getPath(href, url);
                URLDemo.download(path, dir + "/js");
            }
        }
        // 图片
        Elements imgs = doc.select("img");
        for (Element img : imgs) {
            String href = img.attr("src");
            String path = getPath(href, url);
            URLDemo.download(path, dir + "/imgs");
        }
    }

    // 构建 http 完整路径
    private static String getPath(String href, URL url) {
        // href : 1. http://... 完整路径  , 2 /xxx/xxx... 绝对路径 ,  3. xxx/xxx  相对路径
        if(href.startsWith("http")){
            return href;
        } else if(href.startsWith("/")){
            String host = "%s://%s";
            host  = String.format(host, url.getProtocol(), url.getHost());
            return host + href;
        } else {
            // path = http://www.07347.com/jobs/jobs_list/page/1.html
            String path = url.toString();
            path = path.substring(0,  path.lastIndexOf("/") + 1);
            path += href;
            return path;
        }
    }


    // http://www.07347.com/jobs/jobs_list/page/1.html
    public static void captureJobs(String addr) throws IOException{
        URL url = new URL(addr);
        Document doc = Jsoup.parse(url, 5000);
        Elements as = doc.select(".td-j-name>a");
        String host = "%s://%s";
        host  = String.format(host, url.getProtocol(), url.getHost());
        for (Element a : as) {
            String href = a.attr("href");
            captureJobPage(host + href);
        }
    }

    public static void captureJobPage(String addr) throws IOException {
        URL url = new URL(addr);
        Document doc = Jsoup.parse(url, 5000);

        // 通过样式选择器, 获取元素
        Element name = doc.selectFirst(".j-n-txt");
        Element wage = doc.selectFirst(".wage");
        Element tit = doc.selectFirst(".item");
        Elements tits = tit.select(".itemli");

        Element type = tits.get(0);
        Element nums = tits.get(1);
        Element dept = tits.get(2);

        Element txt = doc.selectFirst(".describe>.txt");

        System.out.println("name = " + name.text());
        System.out.println("wage = " + wage.text());
        System.out.println("type = " + type.text());
        System.out.println("nums = " + nums.text());
        System.out.println("txt = " + txt.html());

        String id = addr.replaceAll(".*/(\\d+)\\.html","$1");

        List<Map<String, Object>> maps = DBHelper.selectListMap("select id from job where id=?", id);
        if(maps.isEmpty()==false){
            System.out.println("该数据已经采集过了!");
            return;
        }

        DBHelper.update("insert into job values(?,?,?,?,?,?,?)",
                id,name.text(),
                wage.text(),type.text(),dept.text(),
                nums.text(),txt.html());
    }

}
